﻿Imports FinanceiroBLL

Public Class ConsultaPrestadoresFRM
    Dim objBll As New PrestadoresBLL
    

#Region "Cadastrar"
    Private Sub TSBCadastrar_Click(sender As System.Object, e As System.EventArgs) Handles TSBCadastrar.Click
        PrestadoresFRM.Op = "I"
        PrestadoresFRM.ShowDialog()
        carregagrid()
    End Sub
#End Region
#Region "Carrega GRid"
    Sub carregagrid()
        DGVDados.DataSource = objBll.buscar("", 4)
        DGVDados.Refresh()
    End Sub
#End Region


#Region "Fechar"
    Private Sub TSBVoltar_Click(sender As System.Object, e As System.EventArgs) Handles TSBVoltar.Click
        Me.Close()
    End Sub
#End Region
#Region "Limpa"
    Sub Limpar()
        TXTLocalizar.Text = ""
        CBFiltro.SelectedIndex = 1
    End Sub

#End Region

    Private Sub ConsultaPrestadoresFRM_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        carregagrid()
        Limpar()

    End Sub

    Private Sub CBFiltro_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles CBFiltro.KeyPress
        e.Handled = True
    End Sub

    Private Sub TXTLocalizar_TextChanged(sender As System.Object, e As System.EventArgs) Handles TXTLocalizar.TextChanged
        Select Case CBFiltro.Text
            Case "Código"
                DGVDados.DataSource = objBll.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
            Case "Descrição"
                DGVDados.DataSource = objBll.buscar(TXTLocalizar.Text, CBFiltro.SelectedIndex)
        End Select
        If TXTLocalizar.Text = "" Then
            DGVDados.DataSource = objBll.buscar("", 4)
        End If
    End Sub

    Private Sub TXTLocalizar_KeyPress(sender As System.Object, e As System.Windows.Forms.KeyPressEventArgs) Handles TXTLocalizar.KeyPress
        If CBFiltro.SelectedIndex = 0 Then
            If Not Char.IsNumber(e.KeyChar) And Not e.KeyChar = vbBack Then
                e.Handled = True
            End If
        End If
    End Sub

    Private Sub CBFiltro_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBFiltro.SelectedIndexChanged
        If CBFiltro.SelectedIndex = 2 Then
            CBStatus.Visible = True
            TXTLocalizar.Visible = False
            CBStatus.SelectedIndex = 0
        Else
            CBStatus.Visible = False
            TXTLocalizar.Visible = True
        End If
        TXTLocalizar.Text = ""
        carregagrid()
    End Sub

    Private Sub CBStatus_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles CBStatus.SelectedIndexChanged
        DGVDados.DataSource = objBll.buscar(CBStatus.Text, CBFiltro.SelectedIndex)
    End Sub

    Private Sub DGVDados_CellDoubleClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DGVDados.CellDoubleClick
        Try

            '    If ContasPagarFRM.tp = "ret" Then 'retorna ao form q efetuou a chamada
            Dim indice As Integer = -1
            'Percorre O DataGridView Verificando a Linha selecionada
            For Each linha As DataGridViewRow In DGVDados.SelectedRows
                If (Not linha.Cells("ctp_codigo") Is Nothing) Then
                    'Guarda A Posição Selecionada
                    indice = linha.Index
                End If
            Next
            If (indice <> -1) Then
                If (DGVDados.Rows(indice).Cells("ctp_codigo").Value = 1) Then
                    PrestadoresFRM.TNome.Text = DGVDados.Rows(indice).Cells("pre_descricao").Value.ToString()
                    PrestadoresFRM.LBCodigo.Text = DGVDados.Rows(indice).Cells("pre_codigo").Value.ToString()
                    Me.Close()
                Else
                    Throw New Exception("Prestador Inválido!")
                End If
            End If
            ' End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
#Region "Cor de fundo do datagridview"
    Private Sub DGVDados_RowPrePaint(sender As System.Object, e As System.Windows.Forms.DataGridViewRowPrePaintEventArgs) Handles DGVDados.RowPrePaint
        If DGVDados.Rows(e.RowIndex).Index Mod (2) = 1 Then
            DGVDados.Rows(e.RowIndex).DefaultCellStyle.BackColor = Color.Gainsboro
        End If
    End Sub
#End Region
End Class